//
//  YLLHomeTableViewCell.swift
//  YLLSina
//
//  Created by 于玲玲 on 16/8/1.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit
class YLLHomeTableViewCell: UITableViewCell {

    //这个有什么用
   var constrait: Constraint?
    
    
    var cellViewModel:YLLHomeCellViewModel?{
        
        didSet{
            
            orginalView.orginalViewModel = cellViewModel
            footerView.footerViewModel = cellViewModel
            
            constrait?.uninstall()
            
            if cellViewModel?.statusModel?.retweeted_status == nil {
                retweetView.hidden = true
                footerView.snp_updateConstraints(closure: { (make) -> Void in
                    
                    // 记录这个约束,用于下次重用的时候,卸载
                    self.constrait =
                        make.top.equalTo(orginalView.snp_bottom).constraint
                })
            }else{
                retweetView.hidden = false
                
                
                //                // 转发微博的约束也需要更新一下
                footerView.snp_updateConstraints(closure: { (make) -> Void in
                    self.constrait =
                        make.top.equalTo(retweetView.snp_bottom).constraint
                })

            }
        }
        
    }
    

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI(){
       
        contentView.addSubview(orginalView)
        contentView.addSubview(footerView)
        contentView.addSubview(retweetView)
        
        orginalView.snp_makeConstraints { (make) in
            make.left.equalTo(contentView.snp_left)
            make.top.equalTo(contentView.snp_top)
            make.right.equalTo(contentView.snp_right)
//            make.bottom.equalTo(contentView.snp_bottom)
        }
        
        retweetView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.top.equalTo(orginalView.snp_bottom)
            make.right.equalTo(contentView.snp_right)
        }
        footerView.snp_makeConstraints { (make) in
            
//            第一次 我们用 全局来记录一下 footerView 的 top,的约束
            self.constrait = make.top.equalTo(retweetView.snp_bottom).constraint
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
//            make.bottom.equalTo(self.snp_bottom)
            
         
            make.height.equalTo(44)
        }
        
        // 设置自动计算行高的第二步:
        // 设置一下Cell的ContentView
        // 当讲完配图的时候,我们把这些约束去掉,就知道不设置ContentView约束的后果了
        contentView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left)
            make.top.equalTo(self.snp_top)
            make.right.equalTo(self.snp_right)
            // 注意: 自动计算行高的时候,要告诉 contentView的底部到哪个控件
            make.bottom.equalTo(footerView.snp_bottom)
        }

        
    }
    
    //懒加载
    private lazy var orginalView:  YLLOrginalView = YLLOrginalView()
    private lazy var footerView: YLLFooterView = YLLFooterView()
    private lazy var retweetView: YLLRetweetView = YLLRetweetView()
}
